<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

class HomeController extends Controller{
    //数据显示
    public function home_table(Request $request){
        $page=$request->get('page');
        $limit=$request->get('limit');
        $crows=DB::table('index')->get();
        $rows=Db::table('index')->simplePaginate($limit,'*',$limit,$page);
        $data=[
            "code"=>0,
            "msg"=>'',
            "count"=>count($crows),
            "data"=>$rows->items(),
            "limit"=>$limit
        ];
        return json_encode($data);
    }
    //文件上传
    public function upload(Request $request){
        $file = $request->file('img');
        $filename=$file->getClientOriginalName();
        $path=$file->move("blue/images/",$filename);
        $pathname=$path->getPathname();
        return json_encode(['msg'=>$pathname]);
    }
    //添加
    public function home_insert(Request $request){
        $data=$request->post('result');
        $rows=DB::table('index')->insert($data);
        if($rows){
            return 1;
        }
    }
    //修改回显
    public function home_edit($id){
        $rows=DB::table('index')->where('id',$id)->first();
        return view("home.home_edit",['rows'=>$rows]);
    }
    //修改
    public function home_update(Request $request){
        $data=$request->post('result');
        $rows=DB::table('index')->where("id",$data['id'])->update($data);
        if($rows){
            return 1;
        }
    }
    //删除
    public function home_delete(Request $request){
        $id=$request->input('id');
        $rows=DB::table('index')->delete($id);
        if($rows){
            return 1;
        }
    }


}